import webapp2
import jinja2
import os
from google.appengine.ext import ndb
from google.appengine.api import users
import dic
from labelcat import Cat
import person

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))


class City(ndb.Model):
    name = ndb.TextProperty()
    country = ndb.TextProperty()
    archive = ndb.StringProperty()
    era = ndb.TextProperty()
    culture = ndb.TextProperty()
    population = ndb.IntegerProperty()
    money = ndb.IntegerProperty()
    food = ndb.IntegerProperty()
    latitude = ndb.IntegerProperty()
    longitude = ndb.IntegerProperty()
    loyalty = ndb.IntegerProperty()
    scale = ndb.IntegerProperty()

class Index(webapp2.RequestHandler):
    def get(self, lang, archive_name, era_name, country_name, city_name):
        city_mg_map = {}
        instance = dic.Operation()
        dics = instance.getDics(lang, Cat.CITY_MANAGEMENT)
        for dic_entity in dics:
            city_mg_map[dic_entity.name] = dic_entity.label  
        city_dic_name = instance.getLabel(lang, city_name, Cat.City)
        template_values = {
            'country_name':country_name,
            'city_name':city_dic_name,
            'city':city_name,
            'lang':lang,
            'archive':archive_name,
            'era':era_name,
            'country':country_name,
            'cityMgLables':city_mg_map
        }
        
        template = jinja_environment.get_template('city.html')
        self.response.out.write(template.render(template_values))

class TaxPage(webapp2.RequestHandler):
    def get(self, lang, archive_name, era_name, country_name, city_name):
        person_attr_labels = {}
        person_labels = {}
        city_management_Lables = {}
        instance = dic.Operation()
        dics = instance.getDics(lang, Cat.PERSON_ATTR)
        for dic_entity in dics:
            person_attr_labels[dic_entity.name] = dic_entity.label  
        dics = instance.getDics(lang, Cat.PERSON)
        for dic_entity in dics:
            person_labels[dic_entity.name] = dic_entity.label  
        dics = instance.getDics(lang, Cat.CITY_MANAGEMENT)
        for dic_entity in dics:
            city_management_Lables[dic_entity.name] = dic_entity.label  
        
        persons = person.Person.query(person.Person.archive==archive_name, person.Person.city==city_name)    
        
        template_values = {
            'city':city_name,
            'lang':lang,
            'archive':archive_name,
            'era':era_name,
            'country':country_name,
            'persons':persons,
            'person_attr_labels':person_attr_labels,
            'person_labels':person_labels,
            'city_management_Lables':city_management_Lables

        }
        
        template = jinja_environment.get_template('tax-page.html')
        self.response.out.write(template.render(template_values))



class Add(webapp2.RequestHandler):
    def post(self, countryName):
        name = self.request.get("name")
        longitude = self.request.get("longitude")
        latitude = self.request.get("latitude")
        longitude = int(longitude.encode('utf-8'))
        latitude = int(latitude.encode('utf-8'))
        cities = City.query(City.name == name).fetch(1)
        if len(cities) > 0:
            city = cities[0]
            city.latitude = latitude
            city.longitude = longitude
        else:
            city = City.City(name=name,
                        longitude=longitude,
                        latitude=latitude,
                        country=countryName)
        city.put()
        self.redirect("/game/country/" + countryName)

class AddPage(webapp2.RequestHandler):
    def get(self, countryName):
        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login' 
             
        template_values = {
            'url': url,
            'url_linktext': url_linktext,
            'country':countryName
        }
        
        template = jinja_environment.get_template('city-create.html')
        self.response.out.write(template.render(template_values))
